package sgi.client;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import sgi.details_new.AccountInfoDetails;
import sgi.details_new.ImageDetails;
import sgi.details_new.ImageFullDetails;

public class SGIImageEditUI extends SGIClientFrame {
	private JTable table;
	private JPanel contentPane;
	private static SGIImageEditController editReview;
	
	private JTextField txtDate, txtLocation, txtLatitude, txtLongitude,txtSample;
	JFileChooser fileChooser;
	private JPanel Panel;
	private BufferedImage originalImage1;
	private BufferedImage originalImage2;
	private BufferedImage originalImage3;
	private BufferedImage originalImage4;
	private BufferedImage originalImage5;
	private JTextField txtData1;
	private JTextField txtData2;
	private JTextField txtData3;
	private JTextField txtData4;
	private byte[] photo1;
	private byte[] photo2;
	private byte[] photo3;
	private byte[] photo4;
	private byte[] photo5;
	JDatePickerImpl datePickerTo;
	SpringLayout springLayout;
	private SpringLayout sl_datePickerTo;
	private String imageid;
	
	private JButton btnBrowseData1;
	private JButton btnBrowseData2;
	private JButton btnBrowseData3;
	private JButton btnBrowseData4;
	private JButton btnBrowseSample;
	private JButton btnUpdate;
	
	
	/** constructor */
	public SGIImageEditUI()
	{
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 594, 559);		
		
		
		setTitle("Edit Images table");
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		/** return to useraccountmenu */
		JButton btnReturn = new JButton("Return");
		btnReturn.setBounds(10, 485, 117, 33);
		getContentPane().add(btnReturn);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGIImageEditController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		

		

		
		/** setting table  */
		Object[] columnNames = {"imageId"};
		Object rowData[][]={};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames){
	      public boolean isCellEditable(int rowIndex, int mColIndex) {
	          return false;
	        }
	      };
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(485, 23, 88, 461);
		contentPane.add(scrollPane);
		
		table = new JTable(dtm);
		table.setBounds(10, 127, 633, 321);
		
		JTableHeader header = table.getTableHeader();
		
		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.add(table);
		

		
		/** setting size */
		TableColumn column=null;
		for(int i=0;i<table.getColumnCount(); i++)
		{
			column= table.getColumnModel().getColumn(i);
			if(i== 3)
			{

				column.setWidth(200);
			}
			if(i== 4)
			{

				column.setWidth(600);
			}
		}
		Panel = new JPanel();
		contentPane.add(Panel);
		Panel.setLayout(null);
		Panel.setBounds(10, 23, 465, 409);
		
		/** getting date calendar */
		UtilDateModel model = new UtilDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(model);
		datePickerTo = new JDatePickerImpl(datePanel);
		springLayout = new SpringLayout();
		springLayout.putConstraint(SpringLayout.NORTH, datePickerTo.getJFormattedTextField(), 0, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.WEST, datePickerTo.getJFormattedTextField(), -90, SpringLayout.EAST, datePickerTo);
		springLayout.putConstraint(SpringLayout.SOUTH, datePickerTo.getJFormattedTextField(), 23, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.EAST, datePickerTo.getJFormattedTextField(), -35, SpringLayout.EAST, datePickerTo);
		sl_datePickerTo = (SpringLayout) datePickerTo.getLayout();
		datePickerTo.setBounds(63, 15, 145, 26);
		Panel.add(datePickerTo);
		datePickerTo.setVisible(false);
		/** labels and textfields of upadeting image fields */
		JLabel lbldate = new JLabel("Date:");
		lbldate.setBounds(10, 15, 72, 26);
		Panel.add(lbldate);
			
		JLabel lblLocation = new JLabel("Location:");
		lblLocation.setBounds(218, 15, 72, 26);
		Panel.add(lblLocation);
		
		txtLocation = new JTextField();
		txtLocation.setBounds(286, 15, 154, 26);
		txtLocation.setColumns(10);
		Panel.add(txtLocation);
		txtLocation.setEditable(false);
		
		JLabel lblLatitude = new JLabel("Latitude:");
		lblLatitude.setBounds(10, 52, 72, 26);
		Panel.add(lblLatitude);
		
		txtLatitude = new JTextField();
		txtLatitude.setBounds(63, 52, 145, 26);
		txtLatitude.setColumns(10);
		Panel.add(txtLatitude);
		txtLatitude.setEditable(false);
		
		JLabel lblLongitude = new JLabel("Longitude:");
		lblLongitude.setBounds(218, 52, 72, 26);
		Panel.add(lblLongitude);
		
		txtLongitude = new JTextField();
		txtLongitude.setBounds(286, 52, 154, 26);
		txtLongitude.setColumns(10);
		Panel.add(txtLongitude);
		txtLongitude.setEditable(false);
		
		JLabel lblChooseSample = new JLabel("Choose Image Sample:");
		lblChooseSample.setBounds(10, 114, 172, 34);
		Panel.add(lblChooseSample);
		
		txtSample = new JTextField();
		txtSample.setBounds(183, 115, 158, 32);
		Panel.add(txtSample);
		txtSample.setColumns(10);
		txtSample.setEditable(false);
		txtSample.setText("image");
		
		JLabel lblChooseData1 = new JLabel("Choose Image data Layer 1:");
		lblChooseData1.setBounds(10, 159, 172, 34);
		Panel.add(lblChooseData1);
		
		txtData1 = new JTextField();
		txtData1.setColumns(10);
		txtData1.setBounds(183, 158, 158, 32);
		Panel.add(txtData1);
		txtData1.setEditable(false);
		txtData1.setText("image");
		
		JLabel lblChooseData2 = new JLabel("Choose Image data Layer 2:");
		lblChooseData2.setBounds(10, 204, 172, 34);
		Panel.add(lblChooseData2);
		
		txtData2 = new JTextField();
		txtData2.setColumns(10);
		txtData2.setBounds(183, 205, 158, 32);
		Panel.add(txtData2);
		txtData2.setEditable(false);
		txtData2.setText("image");
		
		JLabel lblChooseData3 = new JLabel("Choose Image data Layer 3:");
		lblChooseData3.setBounds(10, 249, 172, 34);
		Panel.add(lblChooseData3);
		
		txtData3 = new JTextField();
		txtData3.setColumns(10);
		txtData3.setBounds(183, 250, 158, 32);
		Panel.add(txtData3);
		txtData3.setEditable(false);
		txtData3.setText("image");
		
		JLabel lblChooseData4 = new JLabel("Choose Image data Layer 4:");
		lblChooseData4.setBounds(10, 299, 172, 34);
		Panel.add(lblChooseData4);

		txtData4 = new JTextField();
		txtData4.setColumns(10);
		txtData4.setBounds(183, 300, 158, 32);
		Panel.add(txtData4);
		txtData4.setEditable(false);
		txtData4.setText("image");
		/** browse buttons of each type of image */
		btnBrowseSample = new JButton("Browse:");
		btnBrowseSample.setVisible(false);
		btnBrowseSample.setBounds(351, 114, 89, 34);
		btnBrowseSample.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					
				txtSample.setText(chooseImage(0));
		}});
		Panel.add(btnBrowseSample);
		
		btnBrowseData1 = new JButton("Browse:");
		btnBrowseData1.setVisible(false);
		btnBrowseData1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData1.setText(chooseImage(1));
		}});
		btnBrowseData1.setBounds(351, 159, 89, 34);
		Panel.add(btnBrowseData1);
		
		btnBrowseData2 = new JButton("Browse:");
		btnBrowseData2.setVisible(false);
		btnBrowseData2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData2.setText(chooseImage(2));
		}});
		btnBrowseData2.setBounds(351, 204, 89, 34);
		Panel.add(btnBrowseData2);
		
		btnBrowseData3 = new JButton("Browse:");
		btnBrowseData3.setVisible(false);
		btnBrowseData3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData3.setText(chooseImage(3));
		}});
		btnBrowseData3.setBounds(351, 249, 89, 34);
		Panel.add(btnBrowseData3);
		
		btnBrowseData4 = new JButton("Browse:");
		btnBrowseData4.setVisible(false);
		btnBrowseData4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtData4.setText(chooseImage(4));
		}});
		btnBrowseData4.setBounds(351, 299, 89, 34);
		Panel.add(btnBrowseData4);
		


		/** updating the image column selected */
		btnUpdate = new JButton("Update");
		btnUpdate.setBounds(160, 358, 158, 40);
		btnUpdate.setVisible(false);
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Date selectedDateTo = (Date)datePickerTo.getModel().getValue();
				if(selectedDateTo == null || txtLocation.getText().equals("")
					|| txtLatitude.getText().equals("") || txtLongitude.getText().equals("") || txtSample.getText().equals("")
					|| txtData1.getText().equals("") || txtData2.getText().equals("") 
					|| txtData3.getText().equals("")|| txtData4.getText().equals(""))
				{
					editReview.FieldNotInserted();
					SGIImageEditController.terminate();
					try {
						SGIImageEditController.create();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else
				{
					editReview.update(Integer.valueOf(imageid),(selectedDateTo.getYear()+1900)+"-"+(selectedDateTo.getMonth()+1)+"-"+(selectedDateTo.getDate())
					,txtLocation.getText(), txtLatitude.getText(), txtLongitude.getText(),
					photo1, photo2, photo3, photo4, photo5 );
					/** after updating, not allowing touching the buttons or text fields */
					txtData4.setEditable(false);
					txtData3.setEditable(false);
					txtData2.setEditable(false);
					txtData1.setEditable(false);
					txtSample.setEditable(false);
					txtLongitude.setEditable(false);
					txtLatitude.setEditable(false);
					txtLocation.setEditable(false);
					btnBrowseData4.setVisible(false);
					btnBrowseData3.setVisible(false);
					btnBrowseData2.setVisible(false);
					btnBrowseData1.setVisible(false);
					btnBrowseSample.setVisible(false);
					datePickerTo.setVisible(false);
					btnUpdate.setVisible(false);
				}
			}
		});
		Panel.add(btnUpdate);
		/** reviewing the fields of imageid selected */
		JButton btnReview = new JButton("Review");

		btnReview.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int sel = table.getSelectedRow();
				imageid=(String) table.getValueAt(sel, 0);
				editReview.MessageFromImageEditUI(Integer.valueOf(imageid));
				/** allowing to modify fields  */
				txtData4.setEditable(true);
				txtData3.setEditable(true);
				txtData2.setEditable(true);
				txtData1.setEditable(true);
				txtSample.setEditable(true);
				txtLongitude.setEditable(true);
				txtLatitude.setEditable(true);
				txtLocation.setEditable(true);
				btnBrowseData4.setVisible(true);
				btnBrowseData3.setVisible(true);
				btnBrowseData2.setVisible(true);
				btnBrowseData1.setVisible(true);
				btnBrowseSample.setVisible(true);
				datePickerTo.setVisible(true);
				btnUpdate.setVisible(true);
				
			}
		});
		btnReview.setBounds(485, 490, 88, 28);
		contentPane.add(btnReview);

	}

	
	/** to know which browse button we chose and to set correctly */
	public String chooseImage(int button1)
	{
		JFileChooser chooser = new JFileChooser();			    
		chooser.setDialogTitle("Choose Your File");
        FileNameExtensionFilter  filterJPEG = new FileNameExtensionFilter("JPEG", "jpeg");
        FileNameExtensionFilter  filterJPG = new FileNameExtensionFilter("JPG", "jpg");
        chooser.addChoosableFileFilter(filterJPG);
        chooser.addChoosableFileFilter(filterJPEG);
        
        chooser.setFileFilter(filterJPG);
        chooser.setAcceptAllFileFilterUsed(false);
        int returnval=chooser.showOpenDialog(SGIImageAddController.getUI());
        if(returnval==JFileChooser.APPROVE_OPTION)
        {
        	File image = chooser.getSelectedFile();
        	switch(button1)
        	{
        		case 0:
        		{
        			try
        			{
        				originalImage1 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage1, "jpg", baos);
        				photo1 = baos.toByteArray();
        				
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 1:
        		{
        			try
        			{
        				originalImage2 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage2, "jpg", baos);
        				photo2 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 2:
        		{
        			try
        			{
        				originalImage3 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage3, "jpg", baos);
        				photo3 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 3:
        		{
        			try
        			{
        				originalImage4 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage4, "jpg", baos);
        				photo4 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        		case 4:
        		{
        			try
        			{
        				originalImage5 = ImageIO.read(image);
        				ByteArrayOutputStream baos = new ByteArrayOutputStream();
        				ImageIO.write(originalImage5, "jpg", baos);
        				photo5 = baos.toByteArray();
        			} catch (IOException e)
        			{
        				System.out.println(e.getMessage());
        			}
        		}break;
        	}
        	return chooser.getSelectedFile().getAbsolutePath();
        }
        else
        	return "image";
 
	}
	/** get controller */
	public SGIImageEditController getAccountInfoController() {
		return editReview;
	}
	/** set controller  */
	public void setImageEditController(SGIImageEditController editReview)
	{
		SGIImageEditUI.editReview = editReview;
	}
	
	/** add rows of table of imageid */
	public void addRows(ImageDetails cmd)
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.insertRow(table.getRowCount(), new Object[] {String.valueOf(cmd.getImageid())});
		table.setModel(dtm);
	}
	/** clearing the table of imageid */
	public void clearTable()
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}	
	/** reviewing the table of imageid */
	public void review()
	{
		SGIImageEditController.editReviewUI.clearTable();
		editReview.EditReview();	
	}
	/** adding all exiting fields of image to text fields */
	public void addImageInfo(ImageFullDetails aid)
	{
		txtLocation.setText(aid.getlocation());
		txtLatitude.setText(aid.getlatitude());
		txtLongitude.setText(aid.getlongitude());
		photo1 = Arrays.copyOf(aid.getoriginalImage1(), aid.getoriginalImage1().length);
		photo2 = Arrays.copyOf(aid.getoriginalImage2(), aid.getoriginalImage1().length);
		photo3 = Arrays.copyOf(aid.getoriginalImage3(), aid.getoriginalImage1().length);
		photo4 = Arrays.copyOf(aid.getoriginalImage4(), aid.getoriginalImage1().length);
		photo5 = Arrays.copyOf(aid.getoriginalImage5(), aid.getoriginalImage1().length);
		
	}
	/** notify */
	public void notify(String str)
	{
		JOptionPane.showMessageDialog(this,str);
	}
}
